package com.dgj.log.module;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.Times;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.dgj.biz.vo.VO;
import com.dgj.log.entities.Log;
import com.dgj.log.service.log.ILogService;
import com.dgj.log.tools.DateUtil;
import com.dgj.log.vo.DefaultValues;
import com.dgj.nutz.module.base.AbstractBaseModule;
import com.dgj.utils.entries.Result;
import com.dgj.utils.web.pager.Pager;

/**
 * @author 崔海军 create at 2015年7月10日 下午3:16:08
 */
@At("/log")
public class LogModule extends AbstractBaseModule {

	@Inject
	private ILogService logServices;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dgj.nutz.module.base.AbstractBaseModule#_getNameSpace()
	 */
	@Override
	public String _getNameSpace() {
		return "logManage";
	}

	@At
	@Ok("vel:log/list.vm")
	@RequiresRoles(VO.InstalledRoles.PLAT_ADMIN)
	public Result list(int page, String appName) {
		Pager<Log> pager = logServices.listByPage(_fixPage(page), DefaultValues.DEFAULT_PAGE_SIZE, appName);
		pager.addParas("appName", appName);
		pager.setUrl(_base() + "/log/list");
		return Result.success().addData("pager", pager);
	}

	@At("/detail")
	@Ok("vel:log/detail.vm")
	@RequiresRoles(VO.InstalledRoles.PLAT_ADMIN)
	public Result detail(String id, String appName) {
		return Result.success().addData("log", Json.toJson(logServices.detail(id, appName), JsonFormat.forLook()));
	}

	@At
	@Ok("vel:log/list.vm")
	@RequiresRoles(VO.InstalledRoles.PLAT_ADMIN)
	public Result search(int page, @Param("..") Log log) {
		if (log.getStartDate() != null && log.getEndDate() != null) {
			if (!DateUtil.compareDate(log.getStartDate(), log.getEndDate())) {
				return Result.fail("开始时间须小于结束时间");
			}
		}
		Pager<Log> pager = logServices.search(_fixPage(page), DefaultValues.DEFAULT_PAGE_SIZE, log, -1);
		pager.setUrl(_base() + "/log/search");
		pager.addParas("appName", log.getAppName());
		pager.addParas("level", log.getLevel());
		pager.addParas("message", log.getMessage());
		return Result.success().addData("pager", pager).addData("log", log);
	}

	@At
	@Ok("vel:log/currentLog.vm")
	@RequiresRoles(VO.InstalledRoles.PLAT_ADMIN)
	public Result currentLog(@Param("..") Log log) {
		Date date = null;
		Pager<Log> pager = logServices.search(_fixPage(0), DefaultValues.DEFAULT_PAGE_SIZE, log, 1);
		List<String> list = new ArrayList<String>();
		for (Log ilog : pager.getEntities()) {
			list.add(Json.toJson(ilog, JsonFormat.forLook()));
			date = ilog.getTimestamp();
		}
		return Result.success().addData("logs", list).addData("startDate", date).addData("appName", log.getAppName());
	}

	@At
	@RequiresRoles(VO.InstalledRoles.PLAT_ADMIN)
	public Result currentLogs(@Param("..") Log log) {
		log.setStartDate(Times.now());
		Pager<Log> pager = logServices.search(_fixPage(0), DefaultValues.DEFAULT_PAGE_SIZE, log, 1);
		List<String> list = new ArrayList<String>();
		Date date = null;
		for (Log ilog : pager.getEntities()) {
			list.add(Json.toJson(ilog, JsonFormat.forLook()));
			date = ilog.getTimestamp();
		}
		return Result.success().addData("logs", list).addData("startDate", date).addData("appName", log.getAppName());
	}
}
